package main
import "fmt"
//空接口

func test(a interface{}){
	fmt.Printf("type:%T\n",a)
}
type A interface{}


func MyPrintA(a interface{}){
	if _,ok:=a.(string);ok{
		fmt.Printf("string类型\n")
	}else if _,ok:=a.(int);ok{
		fmt.Printf("int类型\n")
	}else if _,ok:=a.(bool);ok{
		fmt.Printf("bool类型\n")
	}
}

type Usb interface{
	start()
	end()
}
type Phone struct{
	Name string
}

type Camera struct{
	Name string
}

type Computer struct{
	Name string
}
func (p Phone)start(){
	fmt.Printf("phone:%s start\n",p.Name)
}
func (c Camera)end(){
	fmt.Printf("camera:%s end\n",c.Name)
}	
func (c Camera)start(){
	fmt.Printf("camera:%s start\n",c.Name)
}
func (p Phone)end(){
	fmt.Printf("phone:%s end\n",p.Name)
}

func (com Computer)work(us Usb){
	if _,ok:=us.(Phone);ok{
		us.start()
	}else{
		us.end()
	}
}

func MyPrintB(a interface{}){
	switch a.(type){
	case int:
		fmt.Printf("类型为int\n")
	case string:
		fmt.Printf("类型为string\n")
	case bool:
		fmt.Printf("bool类型\n")
	default:
		fmt.Printf("其他类型\n")
	}
}
func main(){
	//var a A
	var n int=10
	var a A =n
	fmt.Printf("类型:%T\n",a)

	var str string="hello world"
	a =str
	fmt.Printf("类型:%T\n",str)

	test(str)
	test(10)
	slice:=[]string{"hello","world","thank"}
	test(slice)
	sliceA:=[]interface{}{"hello","78","999","world"}
	fmt.Println(sliceA)
	map1:=make(map[string]interface{})
	map1["name"]="zhangsan"
	map1["age"]=18
	map1["married"]=false

	fmt.Printf("%#v\n",map1)

	v,ok:=a.(string)
	if ok==true{
		fmt.Printf("v=%v\n",v)
	}
	MyPrintA(45)
	MyPrintA("hello world")

	MyPrintB(789)
	MyPrintB("world")
	MyPrintB(true)

	phone:=Phone{Name:"华为手机"}
	camera:=Camera{Name:"索尼手机"}
	var com Computer 
	com.work(phone)
	com.work(camera)
}